<template>
  <div class="container">
    <div class="app-container">
      <!-- data属性是数的数据，数据肯定是树形结构 -->
      <!-- props属性是展示规则，label是显示的文字的字段名是什么，children是子集存储的元素在哪个字段里面 -->
      <el-row>
        <el-col :span="20" :offset="2">
          <el-tree :data="list" :props="defaultProps">
            <el-row slot-scope="{ data }" style="width: 100%">
              <el-col :span="4">{{ data.name }}</el-col>
              <el-col :span="2" :offset="13">{{ data.managerName }}</el-col>
              <el-col :span="4" :offset="1">
                <el-dropdown @command="clickHandler($event, data.id)">
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      command="add"
                    >添加子部门</el-dropdown-item>
                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                    <el-dropdown-item command="del">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-tree>
        </el-col>
      </el-row>
    </div>
    <!-- ----------------- -->
    <addDept ref="addDept" @updateList="initData" />
  </div>
</template>
<script>
import { getDepartmentListApi } from '@/api/department'
import { transListToTreeDate } from '@/utils/index'
import addDept from '@/views/department/components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取部门列表
    async initData() {
      // 发起网络请求
      const res = await getDepartmentListApi()
      // 赋值数据
      // console.log(transListToTreeData(res, 0))
      this.list = transListToTreeDate(res, 0)
    },
    // 点击下拉
    async clickHandler(val, rowId) {
      if (val === 'add') {
        // 添加操作
        this.$refs.addDept.showDialog = true
        // 设置ID
        this.$refs.addDept.formData.pid = rowId
      }
      if (val === 'edit') {
        this.$refs.addDept.replvViewData(rowId)
      }
      if (val === 'del') {
        // this.$refs.addDept.showDialog = true
        await this.$refs.addDept.delRow(rowId)
        this.initData()
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
